//
//  JMSegmenView.m
//  VoluntaryStar
//
//  Created by xiaorui on 15/9/5.
//  Copyright (c) 2015年 ios. All rights reserved.
//

#import "JMSegmenView.h"
#import "JMHeader.h"
#import "UIViewExt.h"


@interface JMSegmenView(){
    NSMutableArray *_array;
    UIButton *currBnt;
}

@property (nonatomic, strong) UIButton *btn1;
@property (nonatomic, strong) UIButton *btn2;

@property (nonatomic, strong) UIButton *btn3;

@property (nonatomic, strong) UIButton *btn4;


@end

@implementation JMSegmenView

- (instancetype)initWithFrame:(CGRect)frame itemArray:(NSArray *)array{
    self = [super initWithFrame:frame];
    if(self){
        _array = (NSMutableArray *)array;
        self.backgroundColor = UIColorFromRGB(0xf3f3f3);
        UIImageView *iv = [[UIImageView alloc]initWithFrame:CGRectMake(0,frame.size.height -0.6,UI_SCREEN_WITH, 0.6)];
        iv.backgroundColor = UIColorFromRGB(0xc4c4c4);
        [self addSubview:iv];
    }
    return self;
}
- (UIButton *)btn1
{
    if (!_btn1) {
        CGFloat with = UI_SCREEN_WITH*1.0/_array.count;
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(with *0, 0, with, self.frameHeight - 0.6);
        btn.tag = 0;
        [btn addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [btn setTitleColor:UIColorFromRGB(0x939393) forState:UIControlStateNormal];
        [btn setTitle:_array[0] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:@"projectImage.bundle/seleted_image_n"] forState:UIControlStateNormal];
        //        [btn setImage:[UIImage imageNamed:@"projectImage.bundle/seleted_image_h"] forState:UIControlStateSelected];
        btn.titleLabel.font = [UIFont systemFontOfSize:14];
        btn.titleEdgeInsets = UIEdgeInsetsMake(0,-with + 60, 0, 8);
        btn.imageEdgeInsets = UIEdgeInsetsMake(0,with - 30, 0, 0);
        _btn1 = btn;
    }
    return _btn1;
}

- (UIButton *)btn2
{
    if (!_btn2) {
        CGFloat with = UI_SCREEN_WITH*1.0/_array.count;
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(with *1, 0, with, self.frameHeight - 0.6);
        btn.tag = 1;
        [btn addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [btn setTitleColor:UIColorFromRGB(0x939393) forState:UIControlStateNormal];
        [btn setTitle:_array[1] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:@"projectImage.bundle/seleted_image_n"] forState:UIControlStateNormal];
        //        [btn setImage:[UIImage imageNamed:@"projectImage.bundle/seleted_image_h"] forState:UIControlStateSelected];
        btn.titleLabel.font = [UIFont systemFontOfSize:14];
        btn.titleEdgeInsets = UIEdgeInsetsMake(0,-with + 60, 0, 8);
        btn.imageEdgeInsets = UIEdgeInsetsMake(0,with - 30, 0, 0);

        _btn2 = btn;
    }
    return _btn2;
}

- (UIButton *)btn3
{
    if (!_btn3) {
        CGFloat with = UI_SCREEN_WITH*1.0/_array.count;
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(with *2, 0, with, self.frameHeight - 0.6);
        btn.tag = 2;
        [btn addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [btn setTitleColor:UIColorFromRGB(0x939393) forState:UIControlStateNormal];
        [btn setTitle:_array[2] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:@"projectImage.bundle/seleted_image_n"] forState:UIControlStateNormal];
        //        [btn setImage:[UIImage imageNamed:@"projectImage.bundle/seleted_image_h"] forState:UIControlStateSelected];
        btn.titleLabel.font = [UIFont systemFontOfSize:14];
        btn.titleEdgeInsets = UIEdgeInsetsMake(0,-with + 60, 0, 8);
        btn.imageEdgeInsets = UIEdgeInsetsMake(0,with - 30, 0, 0);
        
        _btn3 = btn;
    }
    return _btn3;
}

- (UIButton *)btn4
{
    if (!_btn4) {
        CGFloat with = UI_SCREEN_WITH*1.0/_array.count;
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(with *3, 0, with, self.frameHeight - 0.6);
        btn.tag = 3;
        [btn addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [btn setTitleColor:UIColorFromRGB(0x939393) forState:UIControlStateNormal];
        [btn setTitle:_array[3] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:@"projectImage.bundle/seleted_image_n"] forState:UIControlStateNormal];
        //        [btn setImage:[UIImage imageNamed:@"projectImage.bundle/seleted_image_h"] forState:UIControlStateSelected];
        btn.titleLabel.font = [UIFont systemFontOfSize:14];
        btn.titleEdgeInsets = UIEdgeInsetsMake(0,-with + 60, 0, 8);
        btn.imageEdgeInsets = UIEdgeInsetsMake(0,with - 30, 0, 0);
        _btn4 = btn;
    }
    return _btn4;
}


-(void)layoutSubviews{
    [super layoutSubviews];
    [self addSubview:self.btn1];
    [self addSubview:self.btn2];
    [self addSubview:self.btn3];
    [self addSubview:self.btn4];
}

-(void)buttonClick:(UIButton *)sender{
    if([self.delegate respondsToSelector:@selector(segmentItemClick: seletedButton:)]){
        [self.delegate segmentItemClick:sender.tag seletedButton:sender];
        [self setNeedsLayout];
    }else{
        @throw [NSException exceptionWithName:NSExpansionAttributeName reason:@"代理没实现" userInfo:nil];
    }
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
